package org.skydiveapplibrary.ui;

import java.util.List;

import org.skydiveapplibrary.R;
import org.skydiveapplibrary.data.Rig;
import org.skydiveapplibrary.data.RigReminder.DueStatus;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.TextView;

public class GearListAdapter extends BaseExpandableListAdapter
{
	// groups
	private static final int CURRENT_GROUP = 0;
	
	// rigs
	private List<Rig> rigs;
	private List<Rig> archivedRigs;
	
	// context
	private Context context;
	
	public GearListAdapter(Context context, List<Rig> rigs, List<Rig> archivedRigs)
	{
		this.context = context;
		this.rigs = rigs;
		this.archivedRigs = archivedRigs;
	}
	
	@Override
	public int getGroupCount()
	{
		return 2;
	}
	
	@Override
	public Object getGroup(int groupPosition)
	{
		return groupPosition;
	}

	@Override
	public long getGroupId(int groupPosition)
	{
		return groupPosition;
	}
	
	@Override
	public View getGroupView(int groupPosition, boolean isExpanded, View view, ViewGroup parent)
	{
		if (view == null)
		{
			view = View.inflate(context, android.R.layout.simple_expandable_list_item_1, null);
		}
		
		// get text
		String text = "";
		if (groupPosition == CURRENT_GROUP)
		{
			text = context.getString(R.string.gear_list_current_label);
		}
		else
		{
			text = context.getString(R.string.gear_list_archived_label);
		}
		
		// set text
		((TextView)view).setText(text);

		return view;
	}
	
	@Override
	public int getChildrenCount(int groupPosition)
	{
		if (groupPosition == CURRENT_GROUP)
		{
			return rigs.size();
		}
		else
		{
			return archivedRigs.size();
		}
	}
	
	@Override
	public long getChildId(int groupPosition, int childPosition)
	{
		if (groupPosition == CURRENT_GROUP)
		{
			return rigs.get(childPosition).getId();
		}
		else
		{
			return archivedRigs.get(childPosition).getId();
		}
	}

	@Override
	public Object getChild(int groupPosition, int childPosition)
	{
		if (groupPosition == CURRENT_GROUP)
		{
			return rigs.get(childPosition);
		}
		else
		{
			return archivedRigs.get(childPosition);
		}
	}
	
	@Override
	public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View view,
			ViewGroup parent)
	{
		// get rig
		Rig rig;
		if (groupPosition == CURRENT_GROUP)
		{
			rig = rigs.get(childPosition);
		}
		else
		{
			rig = archivedRigs.get(childPosition);
		}

		// get text view
		TextView textView = (TextView)View.inflate(context, android.R.layout.simple_list_item_1, null);;
		
		// init text view
		textView.setText(rig.getName());
		textView.setPadding(10, 0, 10, 0);
		
		// update appearance based on due status
		if (!rig.isArchived())
		{
			Drawable icon = null;
			DueStatus dueStatus = rig.getMostUrgentDueStatus();
			if (dueStatus == DueStatus.DueSoon)
			{
				textView.setTextAppearance(context, R.style.DueSoonTextStyle);
			}
			else if (dueStatus == DueStatus.PastDue)
			{
				textView.setTextAppearance(context, R.style.PastDueTextStyle);
				icon = context.getResources().getDrawable(R.drawable.skull_big);
				textView.setCompoundDrawablePadding(5);
				textView.setCompoundDrawablesWithIntrinsicBounds(null, null, icon, null);
			}
		}
		
		return textView;
	}
	
	@Override
	public boolean hasStableIds()
	{
		return true;
	}

	@Override
	public boolean isChildSelectable(int arg0, int arg1)
	{
		return true;
	}
}